/*
 * @Description: 预加载脚本
 * @Date: 2024-09-30 11:37:38
 * @FilePath: \demod:\12electron\b站向军课程\electron-camera\src\preload\index.ts
 */
import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
  // 自定义右键
  rightBtnClick: () => {
    ipcRenderer.send('rightBtnClick') // 使用ipcRenderer发送rightBtnClick事件给主进程
  },
  // 鼠标拖拽窗体
  drag: (opt: { x: number; y: number }) => {
    ipcRenderer.send('drag', opt) // 使用ipcRenderer发送drag事件给主进程
  }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  // 如果开启了进程隔离
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
